package com.spring.oaassist.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.spring.common.web.dao.CareClassAppointmentMapper;
import com.spring.common.web.dao.CourseAppointmentMapper;
import com.spring.common.web.model.CareClassAppointment;
import com.spring.common.web.model.CourseAppointment;
import com.spring.oaassist.web.dao.CourseMapper;
import com.spring.oaassist.web.form.CourseForm;
import com.spring.oaassist.web.form.CourseQueryForm;
import com.spring.oaassist.web.service.CourseService;
import com.spring.security.core.util.UpdateMap;
import com.spring.security.web.dao.CommonMapper;
import com.spring.tools.Result;

/**
 * 课程相关
 * @author wojunyu
 *
 */
@Service
public class CourseServiceImpl implements CourseService {
	
	@Resource
	private CourseMapper courseMapper;
	
	@Resource
	private CommonMapper commonMapper;
	
	@Resource
	private CourseAppointmentMapper courseAppointmentMapper;

	@Resource
	private CareClassAppointmentMapper careClassAppointmentMapper;
	/**
	 * 早教课程预约
	 */
	@Override
	public Result appointCourse(CourseForm courseForm) {
		Result m = new Result();
		//验证课程id
		int count = courseMapper.appointCourseCheckCourseId(courseForm);
		if(count!=1){
			m.setStatus("0");
			m.setMsg("未找到该课程或该课程已不可预约");
			return m;
		}
		
		//验证是否重复预约
		int count2 = courseMapper.appointCourseCheckAppointment(courseForm);
		if(count2>=1){
			m.setStatus("0");
			m.setMsg("无法重复预约");
			return m;
		}
	
//		int count3 = courseMapper.appointCourseCheckPhone(courseForm);
//		if(count3>=1){
//			m.setStatus("0");
//			m.setMsg("无法重复预约");
//			return m;
//		}
		
		//插入申请记录
		int effect=courseMapper.appointCourse(courseForm);
		if(effect!=1){
			m.setStatus("0");
			m.setMsg("预约失败");
			return m;
		}
		return m;
	}

	/**
	 * 托班预约
	 */
	@Override
	public Result appointCareClass(CourseForm courseForm){
		Result m = new Result();
		int count = courseMapper.appointCareClassCheckId(courseForm);
		if(count!=1){
			m.setStatus("0");
			m.setMsg("未找到该课程或该课程已不可预约");
			return m;
		}
		int count2 =courseMapper.appointCareClassCheckDuplicate(courseForm);
		if(count2>=1){
			m.setStatus("0");
			m.setMsg("无法重复预约");
			return m;
		}
		
		int effect =courseMapper.appointCareClass(courseForm);
		if(effect!=1){
			m.setStatus("0");
			m.setMsg("预约失败");
		}

		return m;
	}


	/**
	 * 托班取消预约
	 */
	@Override
	public Result cancelCareClass(String care_class_appointment_id) {
		Result m = new Result();
		if(StringUtils.isEmpty(care_class_appointment_id)){
			m.setStatus("0");
			m.setMsg("课程不可为空");
			return m;
		}

		HashMap<String,Object> hm = new HashMap<String,Object>();
		hm.put("care_class_appointment_id", care_class_appointment_id);
		
		int count = courseMapper.cancelCareClassCheckId(hm);
		if(count!=1){
			m.setStatus("0");
			m.setMsg("该课程已不可取消预约");
			return m;
		}
		int effect =courseMapper.cancelCareClass(hm);
		if(effect!=1){
			m.setStatus("0");
			m.setMsg("操作失败");
		}
		return m;
	}


	@Override
	public Map<String, Object> getMyCareClassList(CourseQueryForm queryForm) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 早教取消预约
	 */
	@Override
	public Result cancelCourse(String course_appointmet_id) {
		Result m = new Result();
		if(StringUtils.isEmpty(course_appointmet_id)){
			m.setStatus("0");
			m.setMsg("课程ID不可为空");
			return m;
		}

		int count = courseMapper.cancelCourseCheckId(course_appointmet_id);
		//检验id
		if (count != 1) {
			m.setStatus("0");
			m.setMsg("该课程已不可取消预约");
			return m;
		}

		int effect = courseMapper.cancelCourse(course_appointmet_id);
		//取消预约
		if(effect!=1){
			m.setStatus("0");
			m.setMsg("操作失败");
		}

		return m;
	}

	@Override
	public Result releaseCourse(String id, String course_type, String isReleased) {
		Result ret = new Result();
		UpdateMap up = null;
		if("1".equalsIgnoreCase(course_type)){
			up = new UpdateMap("course_open_detail");
		}else if("2".equalsIgnoreCase(course_type)){
			up = new UpdateMap("care_class_open");
		}
		up.addField("isReleased", isReleased);
		up.addWhere("id", id);
		int i = commonMapper.update(up);
		if(i==0){
			ret.setMsg("操作失败");
			ret.setStatus("0");
		}else{
			ret.setMsg("操作成功");
			ret.setStatus("1");
		}
		return ret;
	}

	@Override
	public List<CourseAppointment> student(String course_open_detailID) {
		return courseAppointmentMapper.selectByCourseOpenDetailID(course_open_detailID);
	}
	
	@Override
	public List<CareClassAppointment> careClassStudent(String careClassOpenID) {
		return careClassAppointmentMapper.selectByCareClassOpenID(careClassOpenID);
	}

}
